package com.cn.jdia.tas.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.cn.jdia.tas.model.T_inspect;
import com.cn.jdia.tas.service.IInspectService;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

@Controller
@RequestMapping("/control")
public class InspectController {

	@Resource
	private IInspectService service = null;

	@RequestMapping("/inspect")
	public String addInspect(HttpServletRequest request, HttpServletResponse response) throws IOException {

		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();

		String data = request.getParameter("data");

		Gson gson = new Gson();
		Map<String, String> map = gson.fromJson(data, new TypeToken<Map<String, String>>() {
		}.getType());

		String date = map.get("sdate");
		String sttime = map.get("sttime");
		String edtime = map.get("edtime");
		String temvalue = map.get("temvalue");
		String humvalue = map.get("humvalue");
		String upsvalue = map.get("upsvalue");
		String peovalue = map.get("peovalue");
		String content = map.get("content");
		String sixvalue = map.get("sixvalue");
		String sevenvalue = map.get("sevenvalue");
		String selfvalue = map.get("selfvalue");

		System.out.println(date + " " + sttime + " " + edtime + " " + temvalue + " " + humvalue + " " + upsvalue + " "
				+ peovalue + " " + content);
		
		T_inspect inspect=new T_inspect();
		inspect.setCdate(date);
		inspect.setCtime(sttime+" - "+edtime);
		inspect.setTemperature(temvalue);
		inspect.setHumidity(humvalue);
		inspect.setUps(upsvalue);
		inspect.setPeople(peovalue);
		inspect.setContent(content);
		inspect.setWifi6(sixvalue);
		inspect.setWifi7(sevenvalue);
		inspect.setSelf(selfvalue);

		int ret = service.addInspect(inspect);

		out.write(String.valueOf(ret));
		out.flush();
		out.close();
		return null;
	}
	
	@RequestMapping("/inspecthistory")
	public String queryInspect(HttpServletRequest request, HttpServletResponse response) throws IOException {

		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();

		Gson gson = new Gson();
		
		List<T_inspect> topInspectList = service.getTopInspect();
		
		out.write(gson.toJson(topInspectList));
		out.flush();
		out.close();
		return null;
		}

}
